<?php
$baseUrl = Yii::app()->baseUrl;
$cs      = Yii::app()->getClientScript();

$jsFileList = array(
    'browserplus-min',
    'plupload',
    'plupload.html5',
);
foreach ($jsFileList as $file) {
    $path = $baseUrl . '/js/ext/plupload/' . $file . '.js';
    $cs->registerScriptFile($path, 1);
}
?>

<script type="text/javascript">
/*-----------------------------------------------------------
Events
-----------------------------------------------------------*/
var msgLoading = '<?php echo Yii::t('DadminModule.msg', 'loading'); ?>';

$('#btn-cancel, #btn-back').click(function(){
    url = '<?php echo $this->createUrl('cancel'); ?>';
    $.get(url, function(data){
        window.location = '<?php echo $this->createUrl('index'); ?>';
    });
});

$('#btn-reset').click(function(){
    window.location.reload();
})

$('#btn-refresh').click(function(){
    var url = '<?php echo $this->createUrl('clearState') ?>';
    $.post(url, '', function(){
        window.location = "<?php echo $this->createUrl('index'); ?>";
    });
})

$('#btn-search').click(function(){
    $('#search-box').toggleClass('hidden');
    $('#search-box').slideToggle();
})

$('#btn-submit').click(function(){
    url = '<?php echo $this->createUrl('index'); ?>' + '?' + $('#search-form').serialize();
    $.fn.yiiGridView.update('product-grid', {
        url: url
    });
})

$('#cropImage').click(function(){
    url = '<?php echo $this->createUrl('cropImage') ?>' + '/typeId/' + $('#type_id').val();
    
    if ($(this).hasClass('ui-state-default')) {
        loadFrame(url, '#cropImageDialog', 990, 670, '', 'frameForm', 'Crop Image');
    }
});
$('#cropImageDialog').bind('cropImage', function() {
    var params = $(this).data('params');
    url = '<?php echo $this->createUrl('cropImage') ?>' + '/typeId/' + $('#type_id').val();
    
    $.post(url, { crop_info: params }, function(response){
        url = '<?php echo $baseUrl . Yii::app()->params['tempo_url'] ?>'
            + response + '?q=' + Math.floor((Math.random()*100)+1);
        $('#productPickfile_1').html('<img src="' + url + '" />');
        $('#DProduct2_file1').val(response);
        $('#productError_1').empty();
    })
});

/*-----------------------------------------------------------
Functions
-----------------------------------------------------------*/
function updateGridview()
{
  $.fn.yiiGridView.update('newsletter-content-grid');
}

function loadFrame(url, container, width, height, callBack, frameId, title) {
  var frameAttr = 'width="100%" height="100%"'
    + 'marginWidth="0" marginHeight="0"'
    + 'frameBorder="0"';
  
  myDialog = $(container).html('<iframe id="' + frameId + '" '+ frameAttr + '/>').dialog({
    modal: true,
    width: width, height: height,
    position: ['center', 25],
    draggable: false, resizable: false,
    bgiframe: true,
    close: callBack,
    title: title,
    zIndex: 10000
  });
  $("#" + frameId).attr("src", url);

  return myDialog;
};

/*-----------------------------------------------------------------------------
PLUpload
-----------------------------------------------------------------------------*/
var uploader_2 = new plupload.Uploader({
    runtimes : 'html5',
    browse_button : 'productPickfile_2',
    drop_element: 'pluploadContainer',
    url : '<?php echo $this->createUrl('upload', array('type' => 'normal')); ?>',
    // Specify what files to browse for
    file_data_name: 'file_upload',
    multi_selection: false
});
uploader_2.init();
uploader_2.bind('FilesAdded', function(up, file) {
    up.start();
    
    var params = {
        title: 'Uploading',
        msg: '<?php echo Yii::t('DadminModule.msg', 'loading'); ?>'
    };
    $('#null').showLoading(params);    
});

uploader_2.bind('FileUploaded', function(up, file, res) {
    var response = jQuery.parseJSON(res.response);
    $('#null').dialog('close');
    
    if (response.status == 200) {
        url = '<?php echo Yii::app()->baseUrl . Yii::app()->params['tempo_url'] ?>'
            + response.thumb + '?q=' + Math.floor((Math.random()*100)+1);
        $('#productPickfile_2').html('<img src="' + url + '" />');
        $('#DProduct2_file2').val(response.file);
        $('#productError_2').empty();
        
        if ($('#cropImage').hasClass('ui-state-hover')) {
            $('#cropImage').removeClass('ui-state-hover');
            $('#cropImage').addClass('ui-state-default');
        }
    } else {
        $('#productError_2').empty().html(response.message);
    }
});
/*-----------------------------------------------------------------------------
//PLUpload
-----------------------------------------------------------------------------*/

//----------- Link helper -----------
$('#dialogLinks').bind('assignLink', function() {
    $('#DProduct2_link').val($(this).data('link'));
});
$('#assign-link').click(function(){
    var url = '<?php echo $this->createUrl('newsletterContent/assignLink') ?>';
    loadFrame(url, '#dialogLinks', 700, 450, '', 'frameLinks', 'Assign Link');
    return false;
});
//----------- //Link helper -----------
</script>
